package com.donkeyx.utils;

import com.donkeyx.pattern.factory.factorymethod.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Properties;

public class ConfigUtil {

    public static Object getBean() {
        try {
            InputStream in = Test.class.getClassLoader().getResourceAsStream("Graphical.properties");
            Properties props = new Properties();
            InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
            props.load(inputStreamReader);

            String cName=props.getProperty("factoryClassName");
            //通过类名生成实例对象并将其返回
            Class c=Class.forName(cName);
            Object obj=c.newInstance();
            return obj;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        while (true){
            System.out.println("请输入价格:");
            BufferedReader buf= new BufferedReader(new InputStreamReader(System.in));
            String str = buf.readLine();
            BigDecimal nowPrice=new BigDecimal(str);
            int price = getPrice(nowPrice);
            System.out.println(price);
        }
    }

    public static int getPrice(BigDecimal nowPrice){
        BigDecimal mailFee = new BigDecimal(10).setScale(4, BigDecimal.ROUND_HALF_UP);
        BigDecimal profit =  new BigDecimal(5).setScale(4, BigDecimal.ROUND_HALF_UP);
        BigDecimal youhuiquan = new BigDecimal(5).setScale(4, BigDecimal.ROUND_HALF_UP);
        //换算成人民币
        BigDecimal r2n = new BigDecimal(4.2955).setScale(4, BigDecimal.ROUND_HALF_UP);  //1人民币换算新台币
        BigDecimal n2r = new BigDecimal(0.2328).setScale(4, BigDecimal.ROUND_HALF_UP);  //1新台币换算人民币

        //计算本金
        BigDecimal nowPriceRMB =  nowPrice.multiply(n2r).subtract( new BigDecimal(16)).setScale(0, BigDecimal.ROUND_UP);

        //按照新公式计算价格
        BigDecimal newPriceRMB = nowPriceRMB.add(mailFee).add(profit).multiply(new BigDecimal(1.1111).setScale(4, BigDecimal.ROUND_HALF_UP)).add(youhuiquan).setScale(4, BigDecimal.ROUND_HALF_UP);

        //换算成新台币
        BigDecimal newPriceNT = newPriceRMB.multiply(r2n).setScale(4, BigDecimal.ROUND_HALF_UP);

        return newPriceNT.setScale(0, BigDecimal.ROUND_UP).intValue();
    }
}
